package com.liveneo.adapter.mobile.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseRequest;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * 测试控制器（移动端请求）
 * 
 * @author liveneo
 *
 */
@Controller
@RequestMapping("/mobile")
public class MobileTestController extends BaseController {
    /**
     * 测试一（不走MobileHttpFilter过滤器，需要在日志中手动记录输入参数）
     * @param data
     * @return
     */
    // @ResponseBody
    // @RequestMapping("/test")
    // public String test(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data, HttpServletRequest
    // request) {
    // if (StringUtils.isBlank(data)) {
    // // 当无参数[data]时，记录一下当前传入的参数，以便出现此问题时的问题解决
    // LogUtils.error("所有参数为【" + RequestUtil.convertParamtersToStr(request) + "】");
    // BaseResponse baseResponse =
    // MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM,MobileReturnCodeSupport.SYS_CODE,
    // MobileReturnCodeSupport.CODE_ERR_PARAM_NO_EXISTS);
    // String result = JsonUtils.toJson(baseResponse);
    // // 日志记录响应json
    // LogUtils.error("返回结果【" + result + "】");
    // // 无参数时，返回错误信息
    // return EncryptUtil.encodeBase64(result);
    // }
    // LogUtils.info("原始参数值：【" + data + "】");
    // BaseResponse baseResponse = null;
    // String result = "";
    // try {
    // // 通过base64解密
    // String jsonData = EncryptUtil.decodeBase64String(data);
    // LogUtils.info("解密后参数值：【" + jsonData + "】");
    // BaseRequest baseRequest = JsonUtils.fromJson(jsonData, BaseRequest.class);
    // baseResponse =
    // MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS,MobileReturnCodeSupport.SYS_CODE,
    // null);
    // // 覆盖成功信息的值
    // baseResponse.setExtMessage("成功，接收参数: " + baseRequest + "】");
    // } catch (Exception e) {
    // if (e instanceof UnsupportedEncodingException) {
    // // 参数base64解密时异常错误
    // baseResponse =
    // MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM,MobileReturnCodeSupport.SYS_CODE,
    // MobileReturnCodeSupport.CODE_ERR_PARAM_FAILED_DECRYPT);
    // } else if (e instanceof JsonSyntaxException) {
    // // 参数json转换成对象时异常错误
    // baseResponse =
    // MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM,MobileReturnCodeSupport.SYS_CODE,
    // MobileReturnCodeSupport.CODE_ERR_PARAM_FAILED_TO_OBJ);
    // } else {
    // // 其他内部错误
    // baseResponse =
    // MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,MobileReturnCodeSupport.SYS_CODE,
    // null);
    // }
    // LogUtils.error(baseResponse.getExtMessage(), e);
    // } finally {
    // result = JsonUtils.toJson(baseResponse);
    // // 日志记录响应json
    // LogUtils.error("返回结果【" + result + "】");
    // result = EncryptUtil.encodeBase64(result);
    // }
    // return result;
    // }
    /**
     * 测试二，手机请求
     * 
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public String test1(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "login入参=" + loguuid;
        BaseResponse baseResponse = null;
        String result = "";
        try {
            // 参数通过base64解密，然后转换成对象
            BaseRequest baseRequest = JsonUtils.fromJsonWithBase64(logTitle, data, BaseRequest.class);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
            // 覆盖成功信息的值
            baseResponse.setExtMessage("成功，接收参数: " + baseRequest + "】");
        } catch (Exception e) {
            // 其他内部错误，对于转换错误或解密错误，在过滤器已经做了拦截处理
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
            LogUtils.error(baseResponse.getExtMessage(), e);
        } finally {
            result = JsonUtils.toJson(baseResponse);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(baseResponse.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    @RequestMapping("/test3")
    public String test3() {
        return "test";
    }
}
